package com.yc.book.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class MainUI {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainUI window = new MainUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(500, 350);
		shell.setText("图书管理");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("基础数据");
		
		Menu menu_1 = new Menu(menuItem);
		menuItem.setMenu(menu_1);
		
		MenuItem menuItem_1 = new MenuItem(menu_1, SWT.NONE);
		menuItem_1.setText("分类管理");
		
		MenuItem menuItem_2 = new MenuItem(menu_1, SWT.NONE);
		menuItem_2.setText("图书管理");
		
		MenuItem menuItem_3 = new MenuItem(menu_1, SWT.NONE);
		menuItem_3.setText("用户查询");
		
		MenuItem menuItem_4 = new MenuItem(menu, SWT.CASCADE);
		menuItem_4.setText("报表统计");
		
		Menu menu_2 = new Menu(menuItem_4);
		menuItem_4.setMenu(menu_2);
		
		MenuItem menuItem_1_1 = new MenuItem(menu_2, SWT.NONE);
		menuItem_1_1.setText("阅读量统计");
		
		MenuItem menuItem_2_1 = new MenuItem(menu_2, SWT.NONE);
		menuItem_2_1.setText("活跃度统计");
		
		MenuItem menuItem_3_1 = new MenuItem(menu_2, SWT.NONE);
		menuItem_3_1.setText("逾期统计");
		
		MenuItem menuItem_5 = new MenuItem(menu, SWT.CASCADE);
		menuItem_5.setText("帮助");
		
		Menu menu_3 = new Menu(menuItem_5);
		menuItem_5.setMenu(menu_3);
		
		MenuItem menuItem_1_2 = new MenuItem(menu_3, SWT.NONE);
		menuItem_1_2.setText("使用帮助");
		
		MenuItem menuItem_2_2 = new MenuItem(menu_3, SWT.NONE);
		menuItem_2_2.setText("关于我们");
		
		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		
		TabItem tbtm_1 = new TabItem(tabFolder, SWT.NONE);
		tbtm_1.setText("首页");
		
		Label label = new Label(tabFolder, SWT.NONE);
		label.setImage(SWTResourceManager.getImage("C:\\Users\\Ading\\Pictures\\联想截图\\lenovo20221021212604.png"));
		tbtm_1.setControl(label);
		
		
		
		menuItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//获取所有标签页
				TabItem[] item = tabFolder.getItems();
				for (int i = 0; i < item.length; i++) {
					if(item[i].getText().equals("图书管理")) {
						//如果已经打开，显示该页
						tabFolder.setSelection(i);
						return;
					}
				}
				TabItem tablItem = new TabItem(tabFolder,SWT.NONE);
				tablItem.setText("图书管理");
				BookMgrCmp bmc = new BookMgrCmp(tabFolder,SWT.NONE);
				tablItem.setControl(bmc);
				tabFolder.setSelection(tablItem);
			}
		});
	
	}
}
